<?php
/**
 * Created by PhpStorm.
 * User: XZR
 * Date: 2018/8/1
 * Time: 15:21
 */

namespace Api\Controller;


//评论控制器


use Api\Model\MedicalCommentLikeModel;
use Api\Model\MedicalCommentModel;
use Api\Model\MedicalRecordCollectModel;
use Api\Model\MedicalRecordModel;
use Api\Model\UserModel;
use Api\Model\UserVoteModel;
use Api\Model\WeixinUserModel;
use Common\Service\WeChatService;

class CommentController extends AuthController
{

    //添加评论
    public function commentSubmit()
    {
        $this->validateParam(array("record_id", 'comment', 'is_show_name'));
        $record_id = $this->getPostParam('record_id');
        $comment = $this->getPostParam('comment');
        $is_show_name = $this->getPostParam('is_show_name');
        $data = [
            'user_id' => $this->userId,
            'record_id' => $record_id,
            'is_show_name' => $is_show_name,
            'comment' => $comment,
            'create_time' => date('Y-m-d H:i:s', time())
        ];
        $insert_id = MedicalCommentModel::singleton()->add($data);
        //病历评论数+1
        MedicalRecordModel::singleton()->incNewCommentCount($record_id);
        //判断是否第一次提交病历,是的推送模板消息
        $count = MedicalCommentModel::singleton()->getCommentCountByRecordId($record_id);
        if ($count == 1) {
            //发送模板消息
            $record = MedicalRecordModel::singleton()->field(['user_id'])->find($record_id);
            $this->sendMsg($record['user_id'], $record_id, "病历");
        }
        $rst = array();
        if ($insert_id) {
            //投票完访问当前比例
            $rst['result'] = 200;
            $rst['msg'] = '提交成功！';
        } else {
            $rst['result'] = 202;
            $rst['msg'] = '系统繁忙！';
        }

        $this->formatjson($rst);

    }

    //回复评论
    public function replySubmit()
    {
        $this->validateParam(array("record_id", "comment_id", 'comment', 'is_show_name'));
        $record_id = $this->getPostParam('record_id');
        $comment_id = $this->getPostParam('comment_id');
        $comment = $this->getPostParam('comment');
        $is_show_name = $this->getPostParam('is_show_name');
        $data = [
            'user_id' => $this->userId,
            'record_id' => $record_id,
            'is_show_name' => $is_show_name,
            'comment' => $comment,
            'parent_id' => $comment_id,
            'create_time' => date('Y-m-d H:i:s', time())
        ];
        $insert_id = MedicalCommentModel::singleton()->add($data);
        //病历评论数+1
        MedicalCommentModel::singleton()->incCommentCount($comment_id);
        //判断是否第一次回复评论,是的推送模板消息
        $count = MedicalCommentModel::singleton()->getReplyCountByRecordId($comment_id);
        if ($count == 1) {
            //发送模板消息
            $record = MedicalCommentModel::singleton()->field(['user_id'])->find($comment_id);
            $this->sendMsg($record['user_id'], $record_id, "评论");
        }
        $rst = array();
        if ($insert_id) {
            //投票完访问当前比例
            $rst['result'] = 200;
            $rst['msg'] = '提交成功！';
        } else {
            $rst['result'] = 202;
            $rst['msg'] = '系统繁忙！';
        }

        $this->formatjson($rst);
    }

    //评论列表
    public function getListByComment()
    {
        $this->validateParam(array("record_id"));
        $record_id = $this->getPostParam('record_id');
        $pageno = $this->getPostParam("pageno", 1);
        $pageSize = $this->getPostParam("pagesize", 20);
        list($hot_ids, $hot_list, $like_record_id) = MedicalCommentModel::singleton()->getListByHotComment($record_id, $this->userId);
        if ($hot_list) {
            foreach ($hot_list as $key => $value) {
                if ($value['id'] == $like_record_id) {
                    //最热点赞
                    $hot_list[$key]['hottest'] = 1;
                } else {
                    $hot_list[$key]['hottest'] = 0;
                }
            }
        }
        $rst = array();
        $rst['result'] = 200;
        $rst['hot_list'] = $hot_list;
        $rst['list'] = MedicalCommentModel::singleton()->getListByComment($record_id, $pageno, $pageSize, $hot_ids, $this->userId);
        $this->formatjson($rst);
    }


    //回复列表
    public function getListByReply()
    {
        $this->validateParam(array("comment_id", "record_id"));
        $comment_id = $this->getPostParam('comment_id');
        $record_id = $this->getPostParam("record_id");
        $pageno = $this->getPostParam("pageno", 1);
        $pageSize = $this->getPostParam("pagesize", 20);
        $rst = array();
        $floor_user_id = 0;
        $rst['result'] = 200;
        $rst['comment_detail'] = MedicalCommentModel::singleton()->getDetailById($comment_id);
        $floor_user_id=$rst['comment_detail']['user_id'];
        $rst['list'] = MedicalCommentModel::singleton()->getListByReply($comment_id, $pageno, $pageSize, $floor_user_id, $record_id, $this->userId);
        $this->formatjson($rst);
    }

    //评论置顶
    public function commentTop()
    {
        $this->validateParam(array("comment_id", "record_id"));
        $comment_id = $this->getPostParam('comment_id');
        $record_id = $this->getPostParam('record_id');
        $type = $this->getPostParam('type', 1);
        //判断权限只有病例拥有者才能置顶
        $record = MedicalRecordModel::singleton()->find($record_id);
        $author_id = $record['user_id'];
        if ($author_id != $this->userId) {
            $rst = array();
            $rst['result'] = 201;
            $rst['msg'] = '没有权限置顶！';
            $this->formatjson($rst);
        }
        if ($type == 1) {
            //置顶评论
            MedicalCommentModel::singleton()->setTopComment($comment_id, $record_id);
        } else {
            //取消置顶
            MedicalCommentModel::singleton()->setBottomComment($comment_id, $record_id);
        }

        $rst = array();
        $rst['result'] = 200;
        $rst['msg'] = '提交成功！';
        $this->formatjson($rst);
    }


    //点赞
    public function commentLike()
    {
        $this->validateParam(array("comment_id", "type"));
        $comment_id = $this->getPostParam('comment_id');
        $type = $this->getPostParam("type");
        if ($type == 1) {
            //点赞
            if (MedicalCommentLikeModel::singleton()->isCommentLikeBefore($comment_id, $this->userId)) {
                $rst = array();
                $rst['result'] = 201;
                $rst['msg'] = '之前已经点赞过！';
                $this->formatjson($rst);
            }
            $data = [
                'user_id' => $this->userId,
                'comment_id' => $comment_id,
                'create_time' => date('Y-m-d H:i:s', time())
            ];
            $insert_id = MedicalCommentLikeModel::singleton()->add($data);
            //点赞数+1
            MedicalCommentModel::singleton()->incLikeCount($comment_id);
            if ($insert_id) {
                //投票完访问当前比例
                $rst['result'] = 200;
                $rst['msg'] = '提交成功！';
            } else {
                $rst['result'] = 202;
                $rst['msg'] = '系统繁忙！';
            }
            $this->formatjson($rst);
        } else {
            //取消点赞
            $where['user_id'] = $this->userId;
            $where['comment_id'] = $comment_id;
            MedicalCommentLikeModel::singleton()->where($where)->save(['delete_time' => time()]);
            //点赞数-1
            MedicalCommentModel::singleton()->decLiketCount($comment_id);
            $rst = array();
            $rst['result'] = 200;
            $rst['msg'] = '提交成功！';
            $this->formatjson($rst);
        }

    }

    //删除评论
    public function deleteComment(){
        $this->validateParam(array("comment_id"));
        $comment_id = $this->getPostParam('comment_id');
        $record = MedicalCommentModel::singleton()->find($comment_id);
        if($record['user_id'] != $this->userId){
            $rst['result'] = 201;
            $rst['msg'] = '没有权限删除别人的评论！';
            $this->formatjson($rst);
        }

        //判断有没有parent_id,有的话是删除子评论
        //没有是删除主评论
        if ($record['parent_id'] != 0) {
            //子评论
            //病历评论数-1
            MedicalCommentModel::singleton()->decCommentCount($record['parent_id']);
        } else {
            //主评论
            //病历评论数-1
            MedicalRecordModel::singleton()->decNewCommentCount($record['record_id']);
        }

        MedicalCommentModel::singleton()->deleteComment($comment_id);

        $rst['result'] = 200;
        $rst['msg'] = '提交成功！';
        $this->formatjson($rst);

    }

    //发送服务通知
    private function sendMsg($user_id, $record_id, $type)
    {
        $user = UserModel::singleton()->find($user_id);
        $openid = $user['openid'];
        $template_id = "xCAjBGmwLOaG9rK41zWb5cfDOHJIMT0xxSEZIaUEOkA";
        $msg = [
            "touser" => $openid,
            "template_id" => $template_id,
            "page" => "pages/case/case?id=" . $record_id . "&reach=1&share=1",
            "miniprogram_state" => "trial",//体验版
            "data" => [
                "date2" => [
                    "value" => date('Y-m-d H:i:s', time())
                ],
                "thing3" => [
                    "value" => "你的" . $type . "有人回复啦，快来看看"
                ]
            ]
        ];
        (new WeChatService())->sendMsg($msg);
    }

}
